package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.TheDishHasSomethingSetmeal;
import com.sky.exception.TheDishIsStatus;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishServise;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiseImpl implements DishServise {
    @Autowired
    private DishMapper dishMapper;

    //修改菜品
    @Override
    public void editDish(DishDTO dishDTO) {
        //封装对象
        Dish dish = new Dish();
        dish.setId(dishDTO.getId());
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setDescription(dishDTO.getDescription());
        dish.setImage(dishDTO.getImage());
        dish.setStatus(dishDTO.getStatus());
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.editDish(dish);
        dishMapper.deleteFlavor(dish.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishMapper.addFlavor(flavor);
            }

        }

    }

    //新增菜品
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        dish.setCreateTime(LocalDateTime.now());
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setCreateUser(BaseContext.getCurrentId());
//        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.adddish(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishMapper.addFlavor(flavor);
            }

        }

    }

    @Override
    public void deleteDish(List<Long> ids) {
        for (Long id : ids) {
            //起售中的菜品不能删除
            Integer status = dishMapper.selectStatus(id);
            if (status == 1){
                throw new TheDishIsStatus("起售中的菜品不能删除");
            }
            //查看菜品是否关联套餐
            Integer i = dishMapper.selectSetmealByDishId(id);
            if (i>0){
                throw new TheDishHasSomethingSetmeal("当前菜品关联了套餐，不能删除");
            }
            dishMapper.deleteDish(id);
            dishMapper.deleteFlavor(id);
        }
    }

    @Override
    public DishDTO list(Long id) {
        //查询菜品
        Dish list = dishMapper.list(id);
        //查询口味
        List<DishFlavor> flavorList = dishMapper.selectlistFlavor(id);
        DishDTO dishDTO = new DishDTO();

        BeanUtils.copyProperties(list,dishDTO);
        BeanUtils.copyProperties(list,dishDTO);
        dishDTO.setFlavors(flavorList);

        return dishDTO;
    }

    @Override
    public List selectByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.selectDishByCategoryId(categoryId);
        return dishes;
    }

    @Override
    public PageResult pageSelect(DishPageQueryDTO dishDTO) {
        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());
        List<DishVO> rows = dishMapper.selectByPage(dishDTO);
        Page<DishVO> p = (Page<DishVO>) rows;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void editStatus(Long id, Integer status) {
        dishMapper.editStatus(id,status);
    }
}
